package cloud.kiko.psn.pojo.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * 通用响应体 2024/7/15 3:27
 */
@ApiModel(description = "通用响应体")
public class R<D>{
	private R(){}
	
	@ApiModelProperty(name = "业务数据")
	private D data;
	@ApiModelProperty(name = "响应状态码", example = "200")
	private int status;
	@ApiModelProperty(name = "响应消息", example = "请求成功")
	private String message;
	
	public D getData(){
		return data;
	}
	
	public void setData(D data){
		this.data = data;
	}
	
	public int getStatus(){
		return status;
	}
	
	public void setStatus(int status){
		this.status = status;
	}
	
	public String getMessage(){
		return message;
	}
	
	public void setMessage(String message){
		this.message = message;
	}
	
	public static <T> R<T> ok(T data,String message){
		R<T> r = new R<>();
		r.setData(data);
		r.setStatus(RSStatus.OK);
		if(message != null&&!message.equals("")){
			r.setMessage(message);
		}else{
			r.setMessage(RMessage.OK_MESSAGE);
		}
		return r;
	}
	
	public static <T> R<T> ok(T data){
		R<T> r = new R<>();
		r.setData(data);
		r.setStatus(RSStatus.OK);
		r.setMessage(RMessage.OK_MESSAGE);
		return r;
	}
	
	public static <T> R<T> ok(String message){
		R<T> r = new R<>();
		r.setStatus(RSStatus.OK);
		if(message != null&&!message.equals("")){
			r.setMessage(message);
		}else{
			r.setMessage(RMessage.OK_MESSAGE);
		}
		return r;
	}
	
	public static <T> R<T> ok(){
		R<T> r = new R<>();
		r.setStatus(RSStatus.OK);
		r.setMessage(RMessage.OK_MESSAGE);
		return r;
	}
	
	public static <T> R<T> notOk(T data,String message){
		R<T> r = new R<>();
		r.setData(data);
		r.setStatus(RSStatus.NOT_OK);
		if(message != null&&!message.equals("")){
			r.setMessage(RMessage.NOT_OK_MESSAGE);
		}else{
			r.setMessage("请求失败");
		}
		return r;
	}
	
	public static <T> R<T> notOk(T data){
		R<T> r = new R<>();
		r.setData(data);
		r.setStatus(RSStatus.NOT_OK);
		r.setMessage(RMessage.NOT_OK_MESSAGE);
		return r;
	}
	
	public static <T> R<T> notOk(String message){
		R<T> r = new R<>();
		r.setStatus(RSStatus.NOT_OK);
		if(message != null&&!message.equals("")){
			r.setMessage(message);
		}else{
			r.setMessage(RMessage.NOT_OK_MESSAGE);
		}
		return r;
	}
	
	public static <T> R<T> notOk(int status){
		R<T> r = new R<>();
		if(status != RSStatus.OK){
			r.setStatus(status);
		}else{
			r.setStatus(RSStatus.NOT_OK);
		}
		r.setMessage(RMessage.NOT_OK_MESSAGE);
		return r;
	}
	
	public static <T> R<T> notOk(){
		R<T> r = new R<>();
		r.setStatus(RSStatus.NOT_OK);
		r.setMessage(RMessage.NOT_OK_MESSAGE);
		return r;
	}
}
